//
//  Checkbox.m
//  ljhoo_ios
//
//  Created by abc123 on 15/9/3.
//  Copyright (c) 2015年 ljhoo. All rights reserved.
//

#import "Checkbox.h"
#import "ToolUtil.h"
#import "Size.h"
@interface Checkbox () {
    ResponseCallback callback;
    UIImageView *imageView;
    UILabel *label;
}

@end

@implementation Checkbox

@synthesize checked;

- (id)initWithParent:(UIView*)_parentView _point:(CGPoint)_point _callback:(ResponseCallback)_callback _text:(NSString*)_text _checked:(BOOL)_checked {
    self = [super init];
    callback = _callback;
    self.checked = _checked;
    float item_size = margin_10 * 2;
    
    CGSize size = ([ToolUtil isBlank:_text] ? CGSizeMake(0, 0) : [_text sizeWithAttributes:@{NSFontAttributeName : FONT(14)}]);
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, item_size, item_size)];
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"checkbox_%@.png", (self.checked ? @"select" : @"normal")]];
    [self addSubview:imageView];
    
//    label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 2, 0, size.width, item_size)];
//    label.font = FONT(14);
//    label.textColor = [ImageUtil colorWithRgb:0x323232];
//    label.text = ([ToolUtil isBlank:_text] ? @"" : _text);
//    [self addSubview:label];
    
    self.frame = CGRectMake(_point.x, _point.y, CGRectGetMaxX(label.frame), item_size);
    [self addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
    [_parentView addSubview:self];
    return self;
}

- (void)doClick:(UIButton*)btn {
    self.checked = !self.checked;
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"checkbox_%@.png", (self.checked ? @"select" : @"normal")]];
    if (nil != callback) {
        callback([NSMutableDictionary dictionaryWithDictionary:@{@"checked" : [NSNumber numberWithBool:self.checked]}]);
    }
}

@end
